﻿using System;
using System.Web.UI.WebControls;
using DMS.BaseClass;
using DMS.Business.Utility;
using DMS.Business.Modules.Organization;

namespace DMS.Web
{
    public partial class Login : BasePage
    {
        protected bool AllowSMSPwd
        {
            get
            {
                return AppParams.AllowSMSPWD;
            }
        }

        private ActorTypes LoginActorType
        {
            get
            {
                return (ActorTypes)Enum.Parse(typeof(ActorTypes), "User");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (AppParams.IsNeedValidCode == 0)
                {
                    this.randomImageTr.Style.Value = "display:none;";
                    this.rfvLoginCode.Enabled = false;
                }
            }
        }

        protected void lbtnLogin_Click(object sender, EventArgs e)
        {
            this.ResetPageStatue();

            if (this.Page.IsValid)
            {
                string userLoginId = this.txtUserName.Text.Trim();
                string userPwdCode = this.txtUserPWD.Text.Trim();
                string loginMsg = string.Empty;

                if (AppParams.IsNeedValidCode == 1)
                {
                    // 随机码检查
                    if (this.txtLoginCode.Text.ToUpper() != Session["RandCode"].ToString().ToUpper())
                    {
                        loginMsg = "校验码不正确";
                        this.SetError(loginMsg);
                        return;
                    }
                }

                AuthenticationUser aUser = null;
                if (this.LoginActorType == ActorTypes.User)
                {
                    aUser = UserHelper.GetUserByLoginID(userLoginId);
                }

                // 用户有效性检查
                if (aUser == null)
                {
                    loginMsg = "用户帐号不存在";
                    this.SetError(loginMsg);
                    return;
                }

                // 用户密码检查
                if (//aUser.ActorType == ActorTypes.SupplierAccount && 
                    !aUser.CheckPassword(userPwdCode))
                {
                    loginMsg = "用户密码错误";
                    this.SetError(loginMsg);
                    return;
                }
               
                // 用户登录
                if (!UserHelper.UserLogin(aUser, LoginTypes.Normal, out loginMsg))
                {
                    this.SetError(loginMsg);
                    return;
                }
            }
        }

        private void ResetPageStatue()
        {
            this.lblErrorMsg.Text = string.Empty;
            this.lblErrorMsg.Visible = false;
        }

        private void SetError(string msg)
        {
            this.lblErrorMsg.Text = msg;
            this.lblErrorMsg.Visible = true;
        }
    }
}